﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using Nhandang;



namespace Nhandangso4
{
    public partial class Form3 : Form
    {
        int x = 0;
        Network nw;
        string ImageBinary = "";
        string path = "";
        //string path2 = "";
        int bo = 1, k = 3;
        string bsx;
        double kqpt;
        WebCam webcam;
        public Form3()
        {
            InitializeComponent();
            webcam = new WebCam();
            webcam.InitializeWebCam(ref pictureBox1);
            webcam.Start();
        }
        
        private void mainWinForm_Load(object sender, EventArgs e)
        {
           
           
        }
        OpenFileDialog op = new OpenFileDialog();
        [DllImport("opendll.dll")]
        public static extern void chuphinh();
        [DllImport("S231.dll", EntryPoint = "GetAge")]
        //
        public static extern int GetAge(int id);

        OleDbConnection con = new OleDbConnection();
        string ketnoi;
        private void button1_Click(object sender, EventArgs e)
        {

            // Create a Bitmap of the same dimension of panelVideoPreview (Width x Height)
            using (Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Get the paramters to call g.CopyFromScreen and get the image
                    Rectangle rectanglePanelVideoPreview = pictureBox1.Bounds;
                  //  Point sourcePoints = pictureBox1.PointToScreen(new Point(pictureBox1.ClientRectangle.X, pictureBox1.ClientRectangle.Y));
                  //  g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size);
                }

                string strGrabFileName = String.Format("D:\\Snapshot_{0:yyyyMMdd_hhmmss}.jpg", DateTime.Now);
               // toolStripStatusLabel1.Text = strGrabFileName;
                bitmap.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            } 
            /* webcam.Stop();
            // Console.WriteLine("This is C# program");
            
             //chuphinh
             chuphinh();
             //tach bien va lay so ky tu cua bien so xe
             int x = GetAge(1);
             // MessageBox.Show("age = " + x);
             IplImage aimg = cvlib.cvLoadImage("index.jpg");
             pictureBox1.Image = (Bitmap)aimg;
             //CvCapture pCapturedImage = cvlib.cvCreateCameraCapture(0);

             //// Get the frame
             //IplImage img = cvlib.cvQueryFrame(pCapturedImage);
             //  pictureBox1.Image = (Bitmap)img;
             // Save the frame into a file
             // cvlib.cvSaveImage("images1.jpg", img); 

             try
             {
                 ketnoi = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=D:\\gara.mdb";
                 con.ConnectionString = ketnoi;
                 con.Open();
                 OleDbDataAdapter da = new OleDbDataAdapter();
                 OleDbCommand cmd = new OleDbCommand();


             }
             catch (Exception ex)
             {
                 //MessageBox.Show(ex.Message);
             }



             */

            //op.Filter = "Image File (*.jpg,*.png)|*.jpg;*.png";
            //if (op.ShowDialog() == DialogResult.OK)
            //{
            //    path = "E:\\Nhandang\\Nhandang\\bin\\Debug\\20 lop 60\\";
            //    IplImage anh = cvlib.cvLoadImage(op.FileName);
            //    ImageBinary = MakeImageBinary.ImageBinary(op.FileName, "0");
            //    pictureBox1.Image = (Bitmap)anh;
            //    //path2 = path + bo + "wafasdf";
            //    //MessageBox.Show(path2);
            //    StreamReader reader = File.OpenText(path + "outputW.ann");
            //    int hiden = -1;
            //    try
            //    {
            //        string[] string_ = reader.ReadLine().Split(',');
            //        reader.Close();
            //        foreach (string s in string_)
            //        {
            //            hiden = ++hiden;
            //            //MessageBox.Show(hiden.ToString());
            //        }
            //    }
            //    catch (Exception) { }
            //    // MessageBox.Show(hiden.ToString());
            //    nw = new Network(256, hiden);
            //    nw.Initialise(path + "hiddenW.ann", path + "outputW.ann");




            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {

            IplImage anh1 = cvlib.cvLoadImage("images.jpg");
            pictureBox1.Image = (Bitmap)anh1;//load anh goc ra picture box
            string pathbs;//
            path = "D:\\neurofile";
            //cat hinh va lan luot dua vao xu ly\
            MessageBox.Show(path, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // neu nhu trong khi < k thi tiep tuc
            while (bo < k)
            {
                int kq = 0;
                kqpt = 0;

                pathbs = "chuso" + bo + ".jpg";
                MessageBox.Show(pathbs, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //mvoermoemob
                ImageBinary = MakeImageBinary.ImageBinary(pathbs, "0");



                int i = 0;
                int kt = 0;
                if (bo != 3)
                {
                    kt = 10;
                }
                else
                {
                    i = 10;
                    kt = 31;
                }
                // while (i < 31)
                if (bo != 3)
                    while (i < kt)
                    {
                        // MessageBox.Show(path);                    
                        //  MessageBox.Show(path + "\\" + i + "\\" + "outputW.ann");
                        StreamReader reader = File.OpenText(path + "\\" + i + "\\" + "outputW.ann");
                        int hiden = -1;
                        try
                        {
                            string[] string_ = reader.ReadLine().Split(',');
                            reader.Close();
                            foreach (string s in string_)
                            {
                                hiden = ++hiden;
                                //MessageBox.Show(hiden.ToString());
                            }
                        }
                        catch (Exception) { }
                        // MessageBox.Show(hiden.ToString());
                        nw = new Network(256, hiden);
                        nw.Initialise(path + "\\" + i + "\\" + "hiddenW.ann", path + "\\" + i + "\\" + "outputW.ann");
                        //   nw.Initialise(path + "hiddenW.ann", path + "outputW.ann");



                        if (path == "" || ImageBinary == "")
                        { MessageBox.Show("Chưa chọn ảnh nhận dạng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                        else
                        {
                            double value = nw.Test(ImageBinary);
                            ///textBox1.Text = value.ToString();
                            //MessageBox.Show(value.ToString());
                            if (value > kqpt)
                            {           //lay kq phan tich co do chinh xac cao nhat :)
                                kq = i;
                                kqpt = value;
                            }
                            //if (value >= 0.5)
                            //{
                            //    MessageBox.Show("đúng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //}
                            //else
                            //    MessageBox.Show("sai", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        i = i + 1;
                    }
                switch (kq)
                {
                    case 0: bsx = bsx + "0"; break;
                    case 1: bsx = bsx + "1"; break;
                    case 2: bsx = bsx + "2"; break;
                    case 3: bsx = bsx + "3"; break;
                    case 4: bsx = bsx + "4"; break;
                    case 5: bsx = bsx + "5"; break;
                    case 6: bsx = bsx + "6"; break;
                    case 7: bsx = bsx + "7"; break;
                    case 8: bsx = bsx + "8"; break;
                    case 9: bsx = bsx + "9"; break;
                    case 10: bsx = bsx + ""; break;
                    case 11: bsx = bsx + "a"; break;
                    case 12: bsx = bsx + "b"; break;
                    case 13: bsx = bsx + "c"; break;
                    case 14: bsx = bsx + "d"; break;
                    case 15: bsx = bsx + "e"; break;
                    case 16: bsx = bsx + "f"; break;
                    case 17: bsx = bsx + "g"; break;
                    case 18: bsx = bsx + "h"; break;
                    case 19: bsx = bsx + "k"; break;
                    case 20: bsx = bsx + "l"; break;
                    case 21: bsx = bsx + "m"; break;
                    case 22: bsx = bsx + "n"; break;
                    case 23: bsx = bsx + "p"; break;
                    case 24: bsx = bsx + "r"; break;
                    case 25: bsx = bsx + "s"; break;
                    case 26: bsx = bsx + "t"; break;
                    case 27: bsx = bsx + "u"; break;
                    case 28: bsx = bsx + "v"; break;
                    case 29: bsx = bsx + "x"; break;
                    case 30: bsx = bsx + "y"; break;
                    case 31: bsx = bsx + "z"; break;

                }
                bo++;
            }
            //xuat ra
            textBox3.Text = bsx;
            textBox1.Text = kqpt.ToString();


            MessageBox.Show("Kết quả nhận diện 2 ký tự đầu :" + bsx, "Thông báo");


            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
           // webcam.;
            Application.Exit();
        }
    }
}
